/**
 * Created by wq on 2017/4/13.
 */
import axios from 'axios';
import { API_VERSION, API_NEW_VERSION } from '../api';

var qs = require('qs');

const ORDERS = 'orders';
const ORDERS_USER = '/orders/user';
const ORDERS_NUMBER = 'orders/number';
const ORDERS_NUMBER_USER = '/orders/number/user';
const ORDERS_BROWSES = 'orders/browses';
const STOWAGE_SHIPPING_BY_CITY_ID = 'freight/contract/has';
const FREIGHT_CONTRACT_HAS_CITY_COUNTY = 'freight/contract/has_by_city_and_county_id';

const FREIGHT_PRICE = 'freight/price';
const ORDERS_REVERSE = 'orders/reverse';
const ORDERS_AFFIRM_PRICE = 'orders/affirm_price';
const ORDERS_IS_DELETE = '/orders/is_delete';
const ORDER_ID = 'orders_id';
const ORDER_BY_ORDERNUM = 'orders/byordernum';
const ORDER_FOR_INVOICE = API_VERSION + 'order/for/invoice';
const ORDERS_SELF_EXTRACT = '/orders/self_extract';
const ORDERS_NO_PAGE = API_VERSION + 'orders/no_page';
const ORDERS_LODOP = API_VERSION + 'orders/lodop';
const ORDERS_INFO_FEE_COUNT_DETAIL = API_VERSION + 'orders/info_fee_count/detail';
const ORDERS_AFFIRM_RELEASE = API_VERSION + 'orders/affirm_release';

/**
 * 客户获取订单列表
 * @param params
 * @returns {AxiosPromise}
 */
export const getOrderIdByLodop = ids => {
  return axios.get(ORDERS_LODOP, {params: {ids: ids}});
};

/**
 * 获取自提订单列表
 * @param params
 * @returns {AxiosPromise}
 */
export const searchSelfExtractOrders = params => {
  return axios.get(API_VERSION + ORDERS_SELF_EXTRACT, {params: params});
};

/**
 * 获取订单列表（发票专用）
 */
export const searchOrderForInvoice = params => {
  return axios.get(ORDER_FOR_INVOICE, {params: params});
};

/**
 * 获取订单列表（不分页）
 */
export const searchOrdersNoPage = params => {
  return axios.get(ORDERS_NO_PAGE, {params: params});
};

/**
 * 获取订单记录
 * @param params
 * @returns {AxiosPromise}
 */
export const getOrderTableData = params => {
  return axios.get(API_NEW_VERSION + ORDERS, {params: params});
};

/**
 * 获取订单费用明细
 */
export const getOrderCostDetail = params => {
  return axios.get(API_NEW_VERSION + 'order_cost_details/all', {params: params});
};

/**
 * 客户获取订单列表
 * @param params
 * @returns {AxiosPromise}
 */
export const getOrderTableDataUser = params => {
  return axios.get(API_VERSION + ORDERS_USER, {params: params});
};

/**
 * 获取当天订单
 * @param params
 * @returns {AxiosPromise}
 */
export const getOrderBrowses = params => {
  return axios.get(API_VERSION + ORDERS_BROWSES, {params: params});
};

/**
 * 保存订单
 * @param params
 * @returns {AxiosPromise}
 */
export const saveOrder = (token, params) => {
  return axios.post(API_VERSION + ORDERS + '?token=' + token, params);
};

/**
 * 修改订单
 * @param token
 * @param params
 * @returns {AxiosPromise}
 */
export const modifOrder = (token, params) => {
  return axios.put(API_VERSION + ORDERS + '?token=' + token, params);
};

/**
 * 确认放货
 * @param token
 * @param params
 * @returns {AxiosPromise}
 */
export const affirmRelease = (token, params) => {
  return axios.post(ORDERS_AFFIRM_RELEASE + '?token=' + token, params);
};

/**
 * 根据id获取订单详情
 * @param token
 * @param params
 * @returns {AxiosPromise}
 */
export const getOrder = (token, id) => {
  return axios.get(API_VERSION + ORDER_ID + '?token=' + token + '&id=' + id);
};

export const getOrderByOrderNumber = orderNum => {
  return axios.get(API_VERSION + ORDER_BY_ORDERNUM + '/' + orderNum);
};

/**
 * 查询路线
 * @param params
 * @returns {AxiosPromise}
 */
export const searchLine = params => {
  return axios.get(API_NEW_VERSION + STOWAGE_SHIPPING_BY_CITY_ID, {params: params});
};
/**
 * 根据 市&县，查询路线
 * 用于修改订单用
 * @param params
 * @returns {AxiosPromise}
 */
export const searchLineById = params => {
  return axios.get(API_NEW_VERSION + FREIGHT_CONTRACT_HAS_CITY_COUNTY, {params: params});
};
/**
 * 查询价格
 * @param params
 * @returns {AxiosPromise}
 */
export const searchPrice = params => {
  return axios.get(API_NEW_VERSION + FREIGHT_PRICE, {params: params});
};

/**
 * 更改订单状态
 * @param params
 * @returns {AxiosPromise}
 */
export const orderReverse = (token, id, orderStatus) => {
  return axios.post(API_VERSION + ORDERS_REVERSE, qs.stringify({token: token, id: id, order_status: orderStatus}));
};

/**
 * 专线审核定价价格
 */
export const orderAffirmPrice = (token, id, affirmPrice, affirmPriceBy) => {
  return axios.post(API_VERSION + ORDERS_AFFIRM_PRICE, qs.stringify({token: token, id: id, affirm_price: affirmPrice, affirm_price_by: affirmPriceBy}));
};

/**
 * 更改订单状态new
 * @param params
 * @returns {AxiosPromise}
 */
export const updateOrderStatus = (token, ids, orderStatus) => {
  return axios.post(API_VERSION + 'orders/status', qs.stringify({token: token, ids: ids, order_status: orderStatus}));
};

/**
 * 订单分派+确认&订单分派  new
 * @param token
 * @param id
 * @param assignOrganizationId
 * @param assignOrganizationName
 * @param stowageCoreId
 * @param stowageCoreName
 * @returns {AxiosPromise}
 */
export const orderAssignAndPayableAndTask = (token, orderId, id, name, assignOrganizationId, assignOrganizationName, stowageCoreId, stowageCoreName) => {
  return axios.post(API_VERSION + 'orders/assign/affirm', qs.stringify({
    token: token,
    order_id: orderId,
    id: id,
    name: name,
    assign_organization_id: assignOrganizationId,
    assign_organization_name: assignOrganizationName,
    stowage_core_id: stowageCoreId,
    stowage_core_name: stowageCoreName
  }));
};

/**
 * 逻辑删除
 * @param token
 * @param id
 * @param property
 * @param val
 * @returns {AxiosPromise}
 */
export const deleteOrder = (token, id, val) => {
  return axios.post(API_VERSION + ORDERS_IS_DELETE, qs.stringify({token: token, id: id, is_delete: val}));
};

/**
 *  物理删除/批量
 * @param token
 * @param id
 * @param property
 * @param val
 * @returns {AxiosPromise}
 */
export const notarizeDeleteOrder = (token, ids) => {
  return axios.delete(API_VERSION + ORDERS + '/' + ids);
};

/**
 * 获取订单个状态条数
 * @param params
 */
export const searchOrderNumbers = params => {
  return axios.get(API_NEW_VERSION + ORDERS_NUMBER, {params: params});
};

/**
 * 客户获取订单个状态条数
 * @param params
 * @returns {AxiosPromise}
 */
export const searchOrderNumbersUser = params => {
  return axios.get(API_VERSION + ORDERS_NUMBER_USER, {params: params});
};

/**
 * 检索信息费对应的订单列表
 * @param param
 * @returns {AxiosPromise}
 */
export const searchOrderInfoFeeCountDetail = (param) => {
  return axios.get(ORDERS_INFO_FEE_COUNT_DETAIL, {params: param});
};
